package com.yswl.yeeduo.ui.http;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import android.content.res.AssetManager;

import com.yswl.yeeduo.App;
import com.yswl.yeeduo.Constant;
import com.yswl.yeeduo.http.KHttpUtils;
import com.yswl.yeeduo.tool.KLog;

public class HttpUtils extends KHttpUtils {

	public static String sendPostMethod4Path(String path, Map<String, Object> params, String encoding) throws ClientProtocolException, IOException {

		String url = Constant.HOST_PATH + path;
		return sendPostMethod4Url(url, params, encoding);
	}
	public static String sendGetMethod4Path(String path, Map<String, Object> params,  String encoding) throws ClientProtocolException, IOException {
		
		String url = Constant.HOST_PATH + path;
		return KHttpUtils.sendGetMethod(url, params, encoding);
	}

	public static String sendPostMethod4Url(String url, Map<String, Object> params, String encoding) throws ClientProtocolException, IOException {
		/* 拼接默认参数 */
		 addDefaultParams(params);

		if (KLog.isShow == true && params!= null) {
			StringBuilder sb = new StringBuilder();
			String currurl = url;
			int i = 0;
			for (Map.Entry<String, Object> entry : params.entrySet()) {
				if (i++ == 0) {
					url += "?" + entry.getKey() + "=" + entry.getValue().toString();
				} else {
					url += "&" + entry.getKey() + "=" + entry.getValue().toString();
				}
			}
			sb.append("---url-->>" + currurl + "\n");
			i = 0;
			KLog.e(TAG, sb.toString());
		}
		
		
		String __return = KHttpUtils.sendPostMethod(url, params, encoding);
		
		if (KLog.isShow == true) {
			StringBuilder __sb = new StringBuilder();
			__sb.append("----->>返回结果= " + __return + "\n------------------------------------\n");
			KLog.e(TAG, __sb.toString());
			if (params.get("pageNum") != null) {
				int __pageNum = (Integer) params.get("pageNum");
				url = url + "_" + __pageNum;
			}
			url = url.replace(".do", "");
			url = url.replace(".", "");
		}

		return __return;
	}

	private static void addDefaultParams(Map<String, Object> __params) {
		// __params.put("areaCode", App.getInstance().getAreaCode());
		// __params.put("appName",
		// App.getInstance().getApplicationContext().getPackageName() + "_" +
		// KSystemTool.getAppVersionCode(App.getInstance().getApplicationContext()));
		// __params.put("phoneType", "android_" + KSystemTool.getSdkVersion());
		// String __phoneId =
		// App.getInstance().sharePreferences.getString("phone_id", null);
		// if (__phoneId == null) {
		// __phoneId = KSystemTool.getInstallationId();
		// Editor __editor = App.getInstance().sharePreferences.edit();
		// __editor.putString("phone_id", __phoneId);
		// __editor.commit();
		// }
		//
		// __params.put("phone_id", __phoneId);
	}

	/**
	 * @param __fileName文件名
	 *            （ 带后缀）
	 * @param isThrowException
	 *            是否抛出异常
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static String sendPostMethod4Url(String fileName,  Boolean isThrowException) throws IOException, ClientProtocolException {
		return getJson(fileName, isThrowException);
	}

	/**
	 * 
	 * @param fileName
	 *            文件名（ 带后缀）是在assets文件下的文件名（ 带后缀）
	 * @param __params
	 *            是否抛出异常
	 * @param isThrowException
	 * @return
	 * @throws IOException
	 */
	public static String getJson(String fileName, Boolean isThrowException) throws IOException {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			throw new ClientProtocolException();
		}
/*
		if (__isThrowException = false) {
			// 俩次异常的几率各为1/10-
			if (new Random().nextInt(101) % 15 == 0) {
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				throw new ClientProtocolException();
			}

			if (new Random().nextInt(101) % 15 == 0) {
				throw new IOException();
			}
		}
*/
		String returnString = null;
		KLog.e(TAG, "----------------------->>模拟网络连接  文件名:" + fileName);
		returnString = __getOutputStream(fileName).toString();
		KLog.e(TAG, "----------------------->>模拟网络连接 json数据:" + returnString);

		return returnString;
	}

	private static ByteArrayOutputStream __getOutputStream(String fileName) throws IOException {
		AssetManager assetManager = App.getInstance().getResources().getAssets();
		InputStream is = assetManager.open(fileName);
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] __b = new byte[1024];
		for (int n; (n = is.read(__b)) != -1;) {
			outputStream.write(__b, 0, n);
		}
		outputStream.close();
		is.close();
		// __assetManager.close(); 不能关掉 在程序后面的 适配器中加载布局的时候需要这个
		return outputStream;
	}
	
}
